package com.itechhero.gencode.util.poi;

import com.itechhero.common.utils.poi.ExcelUtil;
import com.itechhero.gencode.domain.GenTable;
import com.itechhero.gencode.domain.GenTableColumn;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: godkey
 * @Date: 2020/12/7 17:05
 */
public class DBExcelUtil extends ExcelUtil {

    public DBExcelUtil(ArrayList list) {
        super(list);
    }

    public void initTableColumnExcel(){
        // 取出一共有多少个sheet.
        int sheetNo = list.size();
        for (int index = 0; index < sheetNo; index++)
        {
            if(index == 0){
                createSheet("总览", index);
                // 创建表信息表头
                createTableHeader();
                for(int i = 0;i < list.size(); i++){
                    // 产生一行
                    fillTableData((GenTable) list.get(i),sheet.createRow(i+1));
                }
                continue;
            }
            GenTable genTable = (GenTable) list.get(index-1);
            createSheet(genTable.getTableName(), index);

            // 创建表信息表头
            createTableHeader();
            // 产生一行
            fillTableData(genTable,sheet.createRow(1));
            // 创建字段信息表头
            creatableDataHeader();

            int column = 3;
            @Valid List<GenTableColumn> columns = genTable.getColumns();
            for(GenTableColumn genTableColumn:columns){
                Row row = sheet.createRow(column++);
                // 产生一行
                fillExcelData(genTableColumn, row);
            }

        }
    }

    /**
     * 创建数据库表表头
     */
    public void createTableHeader(){
        // 产生一行
        Row row = sheet.createRow(0);
        // 设置列宽
        sheet.setColumnWidth(0, 4000);
        // 设置列宽
        sheet.setColumnWidth(1, 4000);
        // 设置列宽
        sheet.setColumnWidth(2, 10000);
        row.setHeight((short)500);
        Cell cell = row.createCell(0);
        cell.setCellValue("表名");
        cell.setCellStyle((CellStyle) styles.get("header"));

        Cell cell1 = row.createCell(1);
        cell1.setCellValue("中文名");
        cell1.setCellStyle((CellStyle) styles.get("header"));

        Cell cell2 = row.createCell(2);
        cell2.setCellValue("表描述");
        cell2.setCellStyle((CellStyle) styles.get("header"));
    }

    /**
     * 填充表信息
     */
    public void fillTableData(GenTable genTable,Row row){

        row.setHeight((short)2000);
        Cell cell = row.createCell(0);
        cell.setCellValue(genTable.getTableName());
        cell.setCellStyle((CellStyle) styles.get("data"));

        Cell cell1 = row.createCell(1);
        cell1.setCellValue(genTable.getBusinessName());
        cell1.setCellStyle((CellStyle) styles.get("data"));

        Cell cell2 = row.createCell(2);
        cell2.setCellValue(genTable.getTableComment());
        cell2.setCellStyle((CellStyle) styles.get("data"));
    }

    /**
     * 创建表头
     */
    public void creatableDataHeader(){
        // 产生一行
        Row row = sheet.createRow(2);
        // 设置列宽
        sheet.setColumnWidth(0, 4000);
        // 设置列宽
        sheet.setColumnWidth(1, 4000);
        // 设置列宽
        sheet.setColumnWidth(2, 10000);
        row.setHeight((short)500);

        Cell cell = row.createCell(0);
        cell.setCellValue("字段名");
        cell.setCellStyle((CellStyle) styles.get("header"));

        Cell cell2 = row.createCell(1);
        cell2.setCellValue("字段类型");
        cell2.setCellStyle((CellStyle) styles.get("header"));

        Cell cell1 = row.createCell(2);
        cell1.setCellValue("字段描述");
        cell1.setCellStyle((CellStyle) styles.get("header"));

    }

    /**
     * 填充表格数据
     */
    public void fillExcelData(GenTableColumn genTableColumn,Row row){
        row.setHeight((short)2000);
        // 产生一行
        Cell cell = row.createCell(0);
        cell.setCellValue(genTableColumn.getColumnName());
        cell.setCellStyle((CellStyle) styles.get("data"));

        Cell cell1 = row.createCell(1);
        cell1.setCellValue(genTableColumn.getColumnType());
        cell1.setCellStyle((CellStyle) styles.get("data"));

        Cell cell2 = row.createCell(2);
        cell2.setCellValue(genTableColumn.getColumnComment());
        cell2.setCellStyle((CellStyle) styles.get("data"));


    }
}
